<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;
use DB;

class ScripController extends BaseController
{
    public function index()
    {
        $scrips = DB::table('user_put_scrip')->orderBy('id', 'desc')->paginate();
        return view('admin.scrip.index', compact('scrips'));
    }

    public function edit(Request $request)
    {
        $scrip = DB::table('user_put_scrip')->where('id', $request->id)->first();
        $scrip->images = DB::table('user_put_scrip_to_image')->where('scrip_id', $scrip->id)->get()->toArray();
        return view('admin.scrip.edit', compact('scrip'));
    }

    public function update(Request $request)
    {
        $data = $this->setCreateUpdateParams($request->all());
        DB::table('user_put_scrip')->where('id', $request->id)->update($data);
        return jsonSuccess();
    }

    private function setCreateUpdateParams($params = [])
    {
        $data = [];
        if (isset($params['sex'])) $data['sex'] = $params['sex'];
        if (isset($params['weixin'])) $data['weixin'] = $params['weixin'];
        if (isset($params['content'])) $data['content'] = $params['content'];
        if (isset($params['status'])) $data['status'] = $params['status'];
        return $data;
    }
}
